package com.zzuli.yxy.task;

import com.zzuli.yxy.entity.Album;
import com.zzuli.yxy.entity.Singer;
import com.zzuli.yxy.entity.Song;
import com.zzuli.yxy.mapper.SingerMapper;
import com.zzuli.yxy.mapper.SongMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * 通过爬虫我们 已经对 歌曲与歌手 歌曲与专辑 关联表填充了数据
 * 现在要通过上面两张表，确立 歌手与专辑的关联信息
 * @auther pony
 * @create 2023-04-18 10:19
 */
public class CreateSingerAlbumTable {
    @Autowired
    private static SongMapper songMapper;

    @Autowired
    private static SingerMapper singerMapper;

    public static void main(String[] args) {
        //1、查询所有歌曲信息
//        LambdaQueryWrapper<Song> qw = new LambdaQueryWrapper<>();
        List<Song> songList = songMapper.selectList(null);

        //2、遍历集合，根据每首歌的 id 查询对应歌曲的歌手信息和专辑信息
        for (Song song : songList) {
            List<Singer> singerList = songMapper.selectSingerBySongId(song.getSongId());

            Album album = songMapper.selectAlbumBySongId(song.getSongId());

            if (!CollectionUtils.isEmpty(singerList) && !Objects.isNull(album)) {
                // 为歌手添加歌曲信息
                for (Singer singer : singerList) {
                    Integer singerId = singer.getSingerId();
                    Integer albumId = album.getAlbumId();
                    singerMapper.addAlbumForSinger(singerId, albumId);
                }
            }
        }

    }


}
